/* ===================

AX2E (AXESCON XACML 2.0 Engine) is the Java authorization engine, which implements OASIS XACML 2.0 standard.
Copyright (C) 2007 AXESCON LLC

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA


Contact: AXESCON LLC - info{at}axescon.com

 =================== */
package axescon.xacml.engine.alg;


import axescon.xacml.api.CtxHandler;
import axescon.xacml.api.ExpressionEvalIndeterminateException;
import axescon.xacml.api.XacmlException;
import axescon.xacml.api.alg.PolicyCombAlg;
import axescon.xacml.engine.PolicyEvaluatorImpl;
import axescon.xacml.model.ctx.StatusCode;
import axescon.xacml.model.ctx.impl.ResultImpl;
import axescon.xacml.model.ctx.impl.StatusImpl;
import axescon.xacml.model.policy.IdReference;
import axescon.xacml.model.policy.PolicyCombinerParameters;
import axescon.xacml.model.policy.PolicyObject;
import axescon.xacml.model.policy.PolicyProxy;
import axescon.xacml.model.shared.Decision;


/**
 * @author argyn
 *         Date: Jun 27, 2005
 *         Time: 11:09:19 PM
 *         Responsibilities: represents XACML Only one applicable Policy Combining algorithm
 *         see spec 2.0 ch. C.6
 *         id: urn:oasis:names:tc:xacml:1.0:policy-combining-algorithm:only-one-applicable
 */
public class OnlyOneApplicablePolicyAlg implements PolicyCombAlg {

    public OnlyOneApplicablePolicyAlg() {
    }


    /**
     * almost literal pseudo-code implementation
     *
     * @param policies
     * @param req
     * @param ch
     * @param params
     * @return
     */
    public axescon.xacml.model.ctx.Result combine(PolicyProxy[] policies,
                                                  axescon.xacml.model.ctx.XacmlCtx req,
                                                  CtxHandler ch,
                                                  PolicyCombinerParameters[] params,
                                                  PolicyEvaluatorImpl pe) throws XacmlException {
        boolean atLeastOne = false;
        PolicyObject selectedPolicy = null;

        for (int i = 0; i < policies.length; i++) {
            PolicyProxy policy = policies[i];

            try {
                PolicyObject po = null;
                if (policy instanceof IdReference)
                    po = pe.getPolicyReferenceStore().resolve((IdReference) policy);
                else
                    po = (PolicyObject) policy;
                if (pe.getTargetMatcher().match(po.getTarget(), req, ch)) {
                    if (atLeastOne) {
                        ResultImpl res = new ResultImpl();
                        res.setDecision(Decision.BYTE_INDETERMINATE);
                        res.setResourceId(ch.getResourceId(req));
                        res.setStatus(new StatusImpl(StatusCode.INT_PROCESSING_ERR));
                        return res;
                    }

                    atLeastOne = true;
                    selectedPolicy = po;
                }
            } catch (Exception e) {
                ResultImpl res = new ResultImpl();
                res.setDecision(Decision.BYTE_INDETERMINATE);
                res.setResourceId(ch.getResourceId(req));
                if (e instanceof ExpressionEvalIndeterminateException)
                    res.setStatus(((ExpressionEvalIndeterminateException) e).getStatus());
                return res;
            }

        }

        if (atLeastOne)
            return selectedPolicy.evalMatched(pe, req, ch);

        ResultImpl res = new ResultImpl();
        res.setDecision(Decision.BYTE_NOT_APPLICABLE);
        res.setResourceId(ch.getResourceId(req));
        return res;
    }


}
